package com.example.musicplayer.dialogTools;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.musicplayer.R;

//普通的提示对话框
public class NormalAlertDialog extends BaseDialog {
    private TextView mCancel;
    private TextView mConfirm;

    public NormalAlertDialog(Context context, String title, String text) {
        super(context);
        TextView textViewTitle = getDialogView().findViewById(R.id.dialog_normal_alert_title);
        TextView textViewText = getDialogView().findViewById(R.id.dialog_normal_alert_text);
        title = title == null ? "" : title;
        text = text == null ? "" : text;
        textViewTitle.setText(title);
        textViewText.setText(text);
    }

    public NormalAlertDialog(Context context, String title, String text, String leftButton, String rightButton) {
        this(context, title, text);
        if (leftButton != null) {
            mCancel.setText(leftButton);
        }
        if (rightButton != null) {
            mConfirm.setText(rightButton);
        }
    }

    public NormalAlertDialog(Context context, int titleId, int textId, int leftId, int rightId) {
        this(context, context.getResources().getString(titleId)
                , context.getResources().getString(textId)
                , context.getResources().getString(leftId)
                , context.getResources().getString(rightId));
    }

    public void setAdviceButton(boolean isLeftButton) {
        int firstColor = mContext.getResources().getColor(R.color.spring_green);
        int secondColor = mContext.getResources().getColor(R.color.deep_black);
        if (isLeftButton) {
            mCancel.setTextColor(firstColor);
            mConfirm.setTextColor(secondColor);
        } else {
            mCancel.setTextColor(secondColor);
            mConfirm.setTextColor(firstColor);
        }
    }

    public void setDialogTitleSize(int changeSize) {
        if (TITLE_SIZE + changeSize > 0) {
            mTitleSize = TITLE_SIZE + changeSize;
            ((TextView) getDialogView().findViewById(R.id.dialog_normal_alert_title)).setTextSize(mTitleSize);
        }
    }

    public void setDialogTextSize(int changeSize) {
        if (TEXT_SIZE + changeSize > 0) {
            mTextSize = TEXT_SIZE + changeSize;
            ((TextView) getDialogView().findViewById(R.id.dialog_normal_alert_text)).setTextSize(mTextSize);
        }
    }

    public void setDialogButtonSize(int changeSize) {
        if (BUTTON_SIZE + changeSize > 0) {
            mButtonSize = BUTTON_SIZE + changeSize;
            mCancel.setTextSize(mButtonSize);
            mConfirm.setTextSize(mButtonSize);
        }
    }

    public TextView getCancel() {
        return mCancel;
    }

    public TextView getConfirm() {
        return mConfirm;
    }

    public View getLeftButton() {
        return mCancel;
    }

    public View getRightButton() {
        return mConfirm;
    }

    @Override
    protected View setDialogView(Context context) {
        View view = View.inflate(context, R.layout.dialog_normal_alert, null);
        mCancel = view.findViewById(R.id.dialog_normal_alert_cancel);
        mConfirm = view.findViewById(R.id.dialog_normal_alert_confirm);
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        return view;
    }

    @Override
    protected void setDialogLayout(Context context) {
        Window dialogWindow = mDialog.getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = (int) context.getResources().getDimension(R.dimen.dialog_normal_alert_width);
            //lp.height = (int) context.getResources().getDimension(R.dimen.dialog_normal_alert_height);
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.CENTER;
            dialogWindow.setAttributes(lp);
        }
    }
}